package com.tcsl.mylibrary;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.tcsl.mylibrary.utils.AC;

/**
 * 描述:美团支付
 * <p/>作者：wjx
 * <p/>创建时间: 2017/5/26 16:44
 */
public class PayMeiTuan extends TcslPay {

    private static final int PAY = 0x6eff;
    private String app_id;

    public PayMeiTuan(Context context, String app_id) {
        super(context);
        this.app_id = app_id;
    }

    @Override
    protected void pay(Float money, String orderNo, String merchantNo) {
        try {
            Intent i = new Intent();
            int m = Math.round(AC.amountMultiplyToFloat(money, 100));
            i.setComponent(new ComponentName("com.sankuai.poscashier", "com.sankuai.poscashier.activity.cashier.ErpCashierHomeActivity"));
            i.putExtra("amount", m);//支付金额，以分为单位
            i.putExtra("erp_order_id", orderNo);//订单id,传营业No
            i.putExtra("erp_app_id", pkName);//包名
            i.putExtra("erp_desk_id", "");//桌台号
            i.putExtra("erp_app_key", app_id);
            mActivity.startActivityForResult(i, PAY);
        } catch (Exception e) {
            mListener.notSupport();
        }
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PAY) {
            switch (resultCode) {
                //交易成功
                case Activity.RESULT_OK:
                    String no = data.getStringExtra("trace_no");
                    if (TextUtils.isEmpty(no)) {
                        no = "美团智能收银异常数据";
                    }
                    mListener.paySuccess("美团支付成功", no);
                    break;
                case -2:
                    mListener.payError(data.getStringExtra("reason"));
                    break;
                //交易出现问题
                case -3:
                    mListener.payError("交易异常");
            }
            return true;
        } else {
            return false;
        }
    }
}
